package com.dhj.yygh.controller.api;

import com.baomidou.mybatisplus.core.executor.MybatisBatchExecutor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dhj.yygh.common.result.MyResult;
import com.dhj.yygh.common.result.Result;
import com.dhj.yygh.common.utils.AuthContextHolder;
import com.dhj.yygh.enums.OrderStatusEnum;
import com.dhj.yygh.model.order.OrderInfo;
import com.dhj.yygh.model.user.UserInfo;
import com.dhj.yygh.service.OrderInfoService;
import com.dhj.yygh.vo.order.OrderCountQueryVo;
import com.dhj.yygh.vo.order.OrderQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "订单相关 api")
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderApiController {

    @Autowired
    private OrderInfoService orderInfoService;

    @ApiOperation("生成挂号订单")
    @PostMapping("/createOrder/{scheduleId}/{patientId}")
    public MyResult<Long> createOrder(@PathVariable("scheduleId") String scheduleId,
                                      @PathVariable("patientId") Long patientId) {
        Long orderId = orderInfoService.createOrder(scheduleId, patientId);
        return MyResult.ok(orderId);
    }

    @ApiOperation("根据订单 id 获取订单详情")
    @GetMapping("/getOrderDetail/{orderId}")
    public MyResult<OrderInfo> getOrders(@PathVariable("orderId") Long orderId) {
        OrderInfo orderInfo = orderInfoService.getOrderDetail(orderId);

        return MyResult.ok(orderInfo);
    }

    @ApiOperation("分页条件获取列表")
    @GetMapping("/getOrderList/{page}/{limit}")
    public MyResult<Page<OrderInfo>> getOrderList(
            HttpServletRequest req,
            @PathVariable("page") Long page,
            @PathVariable("limit") Long limit,
            OrderQueryVo orderQueryVo) {

        // 客户端只能获取自己的订单列表，首先设置当前用户的 id 作为查询条件
        Long userId = AuthContextHolder.getUserId((req));// 从 token 中获取当前登录用户的 id

        orderQueryVo.setUserId(userId);

        Page<OrderInfo> orderInfoPage = orderInfoService.getOrderList(page, limit, orderQueryVo);

        orderInfoPage.getRecords();

        return MyResult.ok(orderInfoPage);
    }

    @ApiOperation(value = "获取订单状态列表")// 主要用于页面订单状态作为筛选条件时的下拉列表
    @GetMapping("/getStatusList")
    public MyResult getStatusList() {
        return MyResult.ok(OrderStatusEnum.getStatusList());
    }

    @ApiOperation(value = "取消预约订单")
    @GetMapping("/cancelOrder/{orderId}")
    public MyResult<Boolean> cancelOrder(@PathVariable("orderId") Long orderId) {
        Boolean is = orderInfoService.cancelOrder(orderId);
        return MyResult.ok(is);
    }


    @ApiOperation(value = "获取订单统计数据")
    @PostMapping("/getCountMap")
    public Map<String, Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo) {
        return orderInfoService.getOrderCountMap(orderCountQueryVo);
    }
}
